/**
 * StringEncoder.java
 * Created on 11 6, 07
 */
package com.pacm.i18n;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.io.FileUtils;

/**
 * Encodes a string to UTF-8
 *
 * @author Paul Augustine C. Mendoza
 * @since 1.0
 */
public class StringEncoder {

    private final static String INPUT = "raw.txt";

    private final static File WITHOUT_ENCODING = new File("noEncoding.htm");

    private final static File WITH_ENCODING = new File("withEncoding.htm");

    private final static String UTF = "UTF-8";

    /**
     * Main launcher
     */
    public static void main(String[] args) {
        new StringEncoder().export();
    }

    /**
     * Todo: Document me!!!
     */
    private void export() {
        String inputStr = this.getClass().getClassLoader().getResource(INPUT)
                .getPath();
        File inputFile = new File(inputStr);
        try {
            String testStr = FileUtils.readFileToString(inputFile, null);
            FileUtils.writeStringToFile(WITHOUT_ENCODING, testStr, null);
            System.out.println("Done writing to "
                    + WITHOUT_ENCODING.getAbsolutePath());
            FileUtils.writeStringToFile(WITH_ENCODING, toUTF(testStr), UTF);
            System.out.println("Done writing to "
                    + WITH_ENCODING.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns a UTF encoding of the input string
     *
     * @param input
     *            input data <code>java.lang.String</code>
     * @return UTF-encoded string
     * @throws UnsupportedEncodingException
     */
    public static String toUTF(String input)
            throws UnsupportedEncodingException {
        byte[] bytes = input.getBytes(UTF);
        String line = new String(bytes, UTF);
        System.out.println("Line - " + line);
       return line;
    }
}
